import 'dart:core';
import 'dart:io';

import 'package:camera/camera.dart';
import 'package:camera/camera_state.dart';
import 'package:camera/key_listener.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';


typedef TakePhotoCallBack = void Function(XFile file, String md5);

///手机拍照页面
class TakePhotoPage extends StatefulWidget {


  const TakePhotoPage({
    super.key,

  });

  @override
  State createState() => TakePhotoState();
}

class TakePhotoState<T extends TakePhotoPage> extends CameraState<T> {

  String? preImage;
  double screenOpacity = 1;
  bool _hasNewTakePicture = false;
  bool _igonreNoLocation = false;


  KeyListener volumeListener = KeyListener();
  int volumeMode = 0;

  ///0-默认方式；1-横屏拍照；2-竖屏拍照
  int screenMode = 0;
  bool _lock = false;
  bool _shutterSound = false;
  bool _clickRatio = false;
  bool _clickMode = false;
  bool _clickExposure = false;
  bool showTip = true;

  @override
  bool get autoFocus => true;


  @override
  void initState() {
    super.initState();
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
    initCamera(ratio: ImageRatio.find(0));
  }


  @override
  void dispose() {
    volumeListener.dispose();
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: SystemUiOverlay.values);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Widget w = Material(
      color: Colors.black,
      child: WillPopScope(
          onWillPop: () async {
            onBackPressed();
            return false;
          },
          child: Scaffold(
            backgroundColor: Colors.black,
            body: Opacity(
                opacity: screenOpacity,
                child: Stack(children: [
                  Padding(padding: EdgeInsets.only(top: imageRatio == ImageRatio.ratio_16_9? 0 : 90),
                      child: cameraPreviewWidget()),

                  tipWidget()
                ])),
          )),
    );
    return w;
  }

  Widget tipWidget(){
    return Builder(builder: (content) {
      if(showTip) {
        showTip = false;

      }
      return Container();
    });
  }



  closeAllClick({bool mode = false, exposure = false, ratio = false}) {
    _clickMode = mode;
    _clickExposure = exposure;
    _clickRatio = ratio;
    mountedState();
  }








  ///显示拍照的缩略图
  Widget showPictureThumbWidget() {
    if (preImage == null) {
      return const SizedBox(width: 48);
    }
    File file = File(preImage!);
    if(!file.existsSync()) return const SizedBox(width: 48);
    var w = 275;
    return Center(
      child: GestureDetector(

        child: ClipRRect(
          borderRadius: BorderRadius.circular(2),
          child: Image.file(
            file,
            width: 45,
            height: 44,
            cacheHeight: (w / 9.0 * 16).toInt(),
            cacheWidth: w ,
            fit: BoxFit.cover,
          ),
        ),
      ),
    );
  }

  Widget flashButton() => assetImageButton(
      flashMode == FlashMode.torch
          ? 'camera_flash_on.png'
          : 'camera_flash_off.png',
      clickFlash);

  Widget assetImageButton(String pngName, VoidCallback call,
      [bool click = false]) {
    Widget w = InkWell(onTap: call, child: Container());
    if (click) {
      w = Container(
          decoration: BoxDecoration(
              color: Colors.white12, borderRadius: BorderRadius.circular(50)),
          child: w);
    }
    return w;
  }

  void onBackPressed() {
    if(_lock) return;

    if(_hasNewTakePicture){
      // if(widget.caseBean == null) eventBus.fire(SwitchMainTab.Evid);
    }
  }

  void clickFlash() {
    flashMode = flashMode == FlashMode.off ? FlashMode.torch : FlashMode.off;
    setFlashMode(flashMode).then((_) => closeAllClick());
  }

  void clickShutter() {
    _shutterSound = !_shutterSound;

    closeAllClick();
  }

  /// 拍摄清晰度选择
  clickImageRatio(ImageRatio res) {

    setImageRatio(res);
  }

  clickScreenMode(mode) {
    screenMode = mode;

    mountedState();
  }
}
